package thread.safety03;

/**
 * 同步方法
 * 锁对象 ： 实现类的对象 this
 *
 * 静态同步方法 锁对象  类对象(RunnableImpl.class)
 */
public class RunnableImpl implements Runnable{
    private int ticket = 100;
    private static int ticket02 = 100;
    @Override
    public void run() {
       // payTicket();
        staticPayTicket();
    }
    public synchronized void payTicket(){

        while(true){
            //synchronized (this){
                if(ticket > 0){
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println(Thread.currentThread().getName()+"正在卖第"+ticket+"张票");
                    ticket--;
              //  }
            }
        }
    }
    public static /*synchronized*/ void staticPayTicket(){
        while(true){
            //synchronized(RunnableImpl.class){
                if(ticket02 > 0){
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println(Thread.currentThread().getName()+"正在卖第"+ticket02+"张票");
                    ticket02--;
                }
           // }
        }
    }
}
